﻿using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
using System.Threading.Tasks;
using Treely.Data.Model;
using Treely.Data.ViewModels;

namespace Treely.Data.Repositories
{
    public class MathQuizTemplateRepository : DisposableRepository
    {
        public MathQuizTemplateRepository(ApplicationDbContext db)
            : base(db)
        {
        }

        public IQueryable<MathQuizTemplateViewModel> GetMathQuizTemplateViewModel(int id)
        {
            return
                db.MathQuizTemplates.Where(x => x.MathQuizTemplateId == id).Select(x => new MathQuizTemplateViewModel()
                {
                    MathQuizTemplateId = x.MathQuizTemplateId,
                    Name = x.Name,
                    Sections = x.Sections.Select(y => new MathQuizTemplateSectionViewModel()
                    {
                        MathQuizTemplateSectionId = y.MathQuizTemplateSectionId,
                        MathQuizTemplateId = x.MathQuizTemplateId,
                        AnswerType = y.AnswerType,
                        NumberOfFactors = y.NumberOfFactors,
                        NumberOfChoices = y.NumberOfChoices,
                        MinimumFactor = y.MinimumFactor,
                        MaximumFactor = y.MaximumFactor,
                        NumberOfQuestions = y.NumberOfQuestions,
                        SectionSubject = y.SectionSubject,
                    })
                });
        }

        public IQueryable<MathQuizTemplateListingViewModel> GetMathQuizTemplateListings()
        {
            return db.MathQuizTemplates.Select(x => new MathQuizTemplateListingViewModel()
            {
                MathQuizTemplateId = x.MathQuizTemplateId,
                Name = x.Name,
                NumberOfSections = x.Sections.Count(),
                TotalQuestions = x.Sections.Sum(y => y.NumberOfQuestions.GetValueOrDefault()),
            });
        }

        public void AddOrUpdateMathQuizTemplateHeader(MathQuizTemplateViewModel model)
        {
            if (model.MathQuizTemplateId == null)
            {
                var template = Mapper.Map<MathQuizTemplate>(model);
                db.MathQuizTemplates.Add(template);
            }
            else
            {
                var existing = db.MathQuizTemplates.First(x => x.MathQuizTemplateId == model.MathQuizTemplateId);
                existing.Name = model.Name;
            }
        }

        public void DeleteMathQuizTemplate(MathQuizTemplateViewModel model)
        {
            var existing = db.MathQuizTemplates.First(x => x.MathQuizTemplateId == model.MathQuizTemplateId);
            existing.Delete(db.UserId);
        }

        public void AddOrUpdateMathQuizTemplateSection(MathQuizTemplateSectionViewModel model)
        {
            if (model.MathQuizTemplateId == null) throw new ArgumentException("Cannot add section: Need an associated MathQuizTemplateId.");
            if (model.MathQuizTemplateSectionId == null)
            {
                var section = Mapper.Map<MathQuizTemplateSection>(model);
                db.MathQuizTemplateSections.Add(section);
            }
            else
            {
                var existing =
                    db.MathQuizTemplateSections.First(
                        x => x.MathQuizTemplateSectionId == model.MathQuizTemplateSectionId);
                existing.MaximumFactor = model.MaximumFactor;
                existing.MinimumFactor = model.MinimumFactor;
                existing.NumberOfQuestions = model.NumberOfQuestions;
                existing.NumberOfChoices = model.NumberOfChoices;
                existing.NumberOfFactors = model.NumberOfFactors;
                existing.SectionSubject = model.SectionSubject;
                existing.AnswerType = model.AnswerType;
            }
        }
    }
}